from datetime import datetime
from app import db


class UserVerification(db.Model):
    """用户认证表"""
    __tablename__ = 'user_verifications'
    
    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False, comment='用户ID')
    type = db.Column(db.String(50), nullable=False, comment='认证类型')
    status = db.Column(db.Integer, default=1, comment='状态：1待审核2通过3拒绝')
    materials = db.Column(db.Text, comment='认证材料JSON')
    audit_reason = db.Column(db.String(200), comment='审核原因')
    auditor_id = db.Column(db.Integer, comment='审核员ID')
    audit_time = db.Column(db.DateTime, comment='审核时间')
    
    # 时间戳
    created_at = db.Column(db.DateTime, default=datetime.utcnow, comment='创建时间')
    
    def to_dict(self):
        """转换为字典"""
        return {
            'id': self.id,
            'user_id': self.user_id,
            'type': self.type,
            'status': self.status,
            'status_text': self.get_status_text(),
            'materials': self.materials,
            'audit_reason': self.audit_reason,
            'auditor_id': self.auditor_id,
            'audit_time': self.audit_time.strftime('%Y-%m-%d %H:%M:%S') if self.audit_time else None,
            'created_at': self.created_at.strftime('%Y-%m-%d %H:%M:%S') if self.created_at else None
        }
    
    def get_status_text(self):
        """获取状态文本"""
        status_map = {1: '待审核', 2: '通过', 3: '拒绝'}
        return status_map.get(self.status, '未知')
    
    def __repr__(self):
        return f'<UserVerification {self.id}>' 